home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX Installation Tools & Overlays 2001 November
/
SGI IRIX Installation Tools & Overlays 2001 November - Disc 1.iso
/
dist
/
roboinst.idb
/
usr
/
etc
/
bootpadd.z
/
bootpadd
Wrap
Text File
|
2001-10-10
|
5KB
|
233 lines
#! /bin/sh -f
# Copyright 1997-2000, Silicon Graphics, Inc.
# All Rights Reserved.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
# the contents of this file may not be disclosed to third parties, copied or
# duplicated in any form, in whole or in part, without the prior written
# permission of Silicon Graphics, Inc.
#
# RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
# and Computer Software clause at DFARS 252.227-7013, and/or in similar or
# successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
# rights reserved under the Copyright Laws of the United States.
# bootpadd
# add an entry to /usr/etc/bootptab
#
# if any attribute is omitted from the command line, they are
# determined by first looking for an entry for the specified
# host in the /usr/etc/bootptab file, then by system calls
#
# host htype haddr iaddr bootfile
# IRIS 1 01:02:03:8a:8b:8c 192.0.2.1 unix
usage="usage: bootpadd [ -nq ] -h haddr -i iaddr -b bootfile hostname"
PATH=/sbin:/usr/bin:/usr/bsd:/usr/sbin:/etc:/usr/etc:
test "$TMPDIR" = "" && {
TMPDIR=/tmp
export TMPDIR
}
HTYPE=1
BOOTFILE=unix
BOOTPTAB=/etc/bootptab
if test ! -f $BOOTPTAB ; then
BOOTPTAB=/usr/etc/bootptab
fi
GETHOSTBYNAME=/usr/etc/gethostbyname
DBGOUT=-
DEBUG=:
ECHO=echo
ERROR=echo
TMPFILE=$TMPDIR/bootpadd.$$
#
# Process options and arguments
#
bootfile=""
debugmode=""
haddr=""
iaddr=""
dryrun=""
while getopts b:dh:i:nq OPT; do
case $OPT in
b) bootfile="$OPTARG";;
d) DEBUG=echo; DBGOUT=1; debugmode=debug;;
h) haddr="$OPTARG";;
i) iaddr="$OPTARG";;
n) dryrun=yes;;
q) ECHO=:;;
*) $ERROR $usage; exit 2
esac
done
shift `expr $OPTIND - 1`
#
# ---- define subroutines ----
#
# get the IP address for a given host ($1)
# returns the IP address in IPADDR global variable
# returns 1 if got a name
getipaddr()
{
hostname="$1"
if [ ! -x $GETHOSTBYNAME ]; then
$ERROR "$GETHOSTBYNAME not installed, aborting"
exit 1
fi
IPADDR=`$GETHOSTBYNAME $hostname | \
nawk -v a=0 '/^'$hostname' / { a=\$3; exit }
END { print a }'`
}
# $1 is hostname to match
getbootp()
{
hostname="$1"
# create temporary nawk program
echo "BEGIN { found=0 }
/^%%/ { found=1 }
found == 0 || \$1 != \"$hostname\" { continue }
NF > 3 { print \$0 ; exit }" > $TMPFILE
line=`nawk -f $TMPFILE $BOOTPTAB`
rm -f $TMPFILE >/dev/null 2>&1
if [ "$line" = "" ]; then
echo ""
return 1
fi
echo $line
}
#
# ---- end functions ----
#
# get hostname, strip domain name if specified
if [ $# != 1 ]; then
$ERROR $usage
exit 2;
fi
HOSTNAME=`echo $1 | nawk -F. '{print $1}'`
if test ! -s $BOOTPTAB ; then
$ERROR "$BOOTPTAB does not exist, aborting"
exit 1
fi
BOOTPINFO=`getbootp $HOSTNAME`
if [ "$BOOTPINFO" != "" ]; then
$ECHO "Overwriting entry for $HOSTNAME in $BOOTPTAB"
fi
# determine default bootfile
if [ "$bootfile" = "" ]; then
# first check bootptab
bootfile=`echo $BOOTPINFO | nawk '{print $5}'`
$DEBUG "Using bootfile $bootfile from $BOOTPTAB"
fi
if [ "$bootfile" = "" ]; then
# then use default
bootfile=`nawk \
'BEGIN { count=0 }
/^[^#]/ && $NF != 0 {
if (count==1) { print $0; exit }
count++
}
/^%%/ { exit }' $BOOTPTAB`
$DEBUG "bootptab default bootfile is $bootfile"
if [ "$bootfile" = "" ]; then
bootfile=$BOOTFILE
fi
$DEBUG "default bootfile is $bootfile"
fi
# get internet address of host
if [ "$iaddr" = "" ]; then
# first check bootptab
iaddr=`echo $BOOTPINFO | nawk '{print $4}'`
$DEBUG "Using iaddr $iaddr from $BOOTPTAB"
fi
if [ "$iaddr" = "" ]; then
# query database
getipaddr $HOSTNAME
if [ "$IPADDR" = "0" ]; then
$ERROR "Unable to get IP address for $HOSTNAME, aborting"
exit 1
fi
iaddr=$IPADDR
$DEBUG "Using IP address $iaddr"
fi
# get hardware (ethernet) address of host
if [ "$haddr" = "" ]; then
# first check bootptab
haddr=`echo $BOOTPINFO | nawk '{print $3}'`
$DEBUG "Using haddr $haddr from $BOOTPTAB"
fi
if [ "$haddr" = "" ]; then
# query client
haddr=`/usr/bsd/rsh $HOSTNAME -n nvram eaddr 2>/dev/null | nawk '/..:..:..:..:..:../ { print $0 }'`
if [ "$haddr" = "" ]; then
$ERROR "Unable to get hardware address for $HOSTNAME, aborting"
exit 1
fi
$DEBUG "Using hardware address $haddr"
fi
# done with initialization,
# we now have all info for bootptab entry,
# update bootptab info
if [ "$dryrun" = "yes" ]; then
exit 0
fi
nawk "BEGIN { found=0 }
/^%%/ { found=1 }
found == 0 || \$1 != \"$HOSTNAME\" { print \$0 }" $BOOTPTAB >$BOOTPTAB.N
if test ! -s $BOOTPTAB.N ; then
exit 1
fi
if test -x /usr/bin/printf; then
/usr/bin/printf "%-15s 1 %-21s %-15s %s\n" $HOSTNAME $haddr $iaddr $bootfile >>$BOOTPTAB.N
else
echo "$HOSTNAME 1 $haddr $iaddr $bootfile" >>$BOOTPTAB.N
fi
rm -f $BOOTPTAB.O >/dev/null 2>&1
mv $BOOTPTAB $BOOTPTAB.O || {
$ERROR "Cannot write backup file $BOOTPTAB.O"
exit 1
}
mv $BOOTPTAB.N $BOOTPTAB || {
$ERROR "Cannot save new bootptab file $BOOTPTAB"
mv $BOOTPTAB.O $BOOTPTAB
exit 1
}
$ECHO "Added $HOSTNAME to $BOOTPTAB"